﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Goals;
using PMPUtils.Form;

namespace PMP.Goals
{
    public partial class AddGoal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckRequiredFields())
            {
                Messenger.Show(Response, "Please fill all required fields.");
                return;
            }

            String goal = txtGoal.Text.Trim();
            String task = txtTaskID.Text.Trim();
            String owner = ddlOwners.SelectedValue.Trim();
            String priority = ddlPriority.SelectedValue.Trim();
            String progress = ddlStatus.SelectedValue.Trim();
            String dueDate = calDueDate.SelectedDate.ToString("yyyy-MM-dd");
            String origEstimation = txtOrigEst.Text.Trim();
            String sprint = ddlSprints.SelectedValue.Trim();

            // Verify duplicate
            if (GoalsOperator.IsGoalExists(goal, sprint))
            {
                Messenger.Show(Response, "The goal already added.");
                return;
            }

            try
            {
                GoalsOperator.AddNewGoal(goal, task, owner, progress, dueDate, priority, origEstimation, sprint);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Messenger.Show(Response, "Goal Added Successfully.");
            Close();
        }

        protected void txtTaskID_TextChanged(object sender, EventArgs e)
        {
            if (!txtTaskID.Text.Trim().Equals(String.Empty))
            {
                txtOrigEst.Text = String.Empty;
                txtOrigEst.Enabled = false;
            }
        }

        private bool CheckRequiredFields()
        {
            bool fieldsChecked = true;

            if (txtGoal.Text.Trim().Equals(String.Empty) ||
                ddlOwners.SelectedItem.Text.Trim().Equals(String.Empty) ||
                ddlPriority.SelectedValue.Trim().Equals(String.Empty) ||
                ddlStatus.SelectedValue.Trim().Equals(String.Empty) ||
                ddlSprints.SelectedValue.Trim().Equals(String.Empty))
            {
                fieldsChecked = false;
            }

            if (txtTaskID.Text.Trim().Equals(String.Empty) &&
                txtOrigEst.Text.Trim().Equals(String.Empty))
            {
                fieldsChecked = false;
            }

            return fieldsChecked;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Close()
        {
            PopupForm addGoal = new PopupForm(Response);
            addGoal.Close();
        }
    }
}
